namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class models
    {
        // Methods
        public static void Add(model m)
        {
            StringBuilder builder = new StringBuilder();
            m.sortid = GetMax("sortid", "models") + 1;
            builder.Append("insert into models(");
            builder.Append("[name],[link],[sortid]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + m.name + "',");
            builder.Append("'" + m.link + "',");
            builder.Append("" + m.sortid + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete models ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from models where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from models ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by sortid ");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static int GetMax(string field, string table)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select max([" + field + "]) from [" + table + "]");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 1;
            }
            return int.Parse(scalar.ToString());
        }

        public static model GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from models ");
            builder.Append(" where id=" + id);
            model model = new model();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            model.id = id;
            if (dataTable.Rows.Count > 0)
            {
                model.name = dataTable.Rows[0]["name"].ToString();
                model.link = dataTable.Rows[0]["link"].ToString();
                if (dataTable.Rows[0]["sortid"].ToString() != "")
                {
                    model.sortid = int.Parse(dataTable.Rows[0]["sortid"].ToString());
                }
                return model;
            }
            return null;
        }

        public static void Update(model m)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update models set ");
            builder.Append("name='" + m.name + "',");
            builder.Append("link='" + m.link + "',");
            builder.Append("sortid=" + m.sortid + "");
            builder.Append(" where id=" + m.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

